/**   
 * @Title: ChatUiActivity.java 
 * @Package android.framework 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-8-9 下午4:35:04 
 * @version V1.0   
 */
package android.framework.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.framework.app.base.BaseActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import android.framework.R;

/**
 * @ClassName: ChatUiActivity
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-8-9 下午4:35:04
 * 
 */
public class ChatUiActivity extends Activity {

	EditText input;
	ListView history;
	Button send;
	List<String> list = new ArrayList<String>();
	HistotyAdapter adapter;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chatui);
		input = (EditText) findViewById(R.id.chatinput);
		history = (ListView) findViewById(R.id.chathistory);
		send = (Button) findViewById(R.id.send);
		for (int i = 0; i < 20; i++) {
			list.add("talk " + i);
		}
		send.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String text = input.getEditableText().toString();
				if(!TextUtils.isEmpty(text)){
					list.add(input.getEditableText().toString());
					input.setText(null);
					adapter.notifyDataSetChanged();
				}
			}
		});
		adapter = new HistotyAdapter();
		history.setAdapter(adapter);
		history.setSelection(adapter.getCount()-1);
	};

	class HistotyAdapter extends BaseAdapter {

		class Holder {
			TextView tv;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder;
			if (convertView == null) {
				holder = new Holder();
				convertView = LayoutInflater.from(parent.getContext()).inflate(
						android.R.layout.simple_list_item_1, null);
				holder.tv = (TextView) convertView
						.findViewById(android.R.id.text1);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			holder.tv.setText(list.get(position));
			// TODO Auto-generated method stub
			return convertView;
		}

	}

}
